package tribe.vilt.lost.receiver;

import android.content.*;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.Toast;
import tribe.vilt.lost.activity.HomeActivity;
import tribe.vilt.lost.activity.PlayAudioActivity;
import tribe.vilt.lost.service.C2DMService;
import tribe.vilt.lost.util.C2DMessaging;
import tribe.vilt.lost.util.Constants;


/**
 * Created by Tobias Ericsson
 */
public class C2DMReceiver extends BroadcastReceiver {

    private final static String TAG = C2DMReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(context, intent);
        } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(context, intent);
        } else if (intent.getAction().equals("tribe.lost.vilt.c2dm.intent.REGISTRATION")) {
            handleLostRegistration(context, intent);
        }
    }

    private void handleMessage(Context context, Intent intent) {

        String message = intent.getExtras().getString(C2DMessaging.C2DM_MESSAGE_EXTRA);

        Log.d(TAG, "message: " + message);

        Intent playAudioIntent = new Intent(context, PlayAudioActivity.class);
        playAudioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        playAudioIntent.putExtra("message", message);
        context.startActivity(playAudioIntent);
    }

    private void handleLostRegistration(Context context, Intent intent) {
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
            Toast.makeText(context, "Registration failed, try again later.", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "Registration successful!", Toast.LENGTH_SHORT).show();
        }
    }

    private void handleRegistration(Context context, Intent intent) {


        String registrationId = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
            Intent callbackIntent = new Intent(context, HomeActivity.class);
            callbackIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(callbackIntent);

        } else if (intent.getStringExtra("unregistered") != null) {
            // unregistration done, new messages from the authorized sender will be rejected
            Intent unIntent = new Intent(context, C2DMService.class);
            unIntent.setAction(Constants.UNREGISTER);
            context.startService(unIntent);
        } else if (registrationId != null) {
            // Send the registration ID to the 3rd party site that is sending the messages.
            // This should be done in a separate thread.
            // When done, remember that all registration is done.
            Intent serviceIntent = new Intent(context, C2DMService.class);
            serviceIntent.setAction(Constants.REGISTER);
            serviceIntent.putExtra("registration_id", registrationId);
            context.startService(serviceIntent);
        }
        Log.d(TAG, "registration_id: " + registrationId);

    }

    // package
    public static void setRegistrationId(Context context, String registrationId) {
        final SharedPreferences prefs = context.getSharedPreferences(
                C2DMessaging.SHARED_PREFS,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("registration_id", registrationId);
        editor.commit();

    }
}
